﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Entity;
using ZhongKeSite.Service;
using ZhongKeSite.Web.Models;

namespace ZhongKeSite.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class AdController : BaseController
    {
        IAdSlotService adSlotService;
        IAdService adService;
        public AdController(IAdService _adService,IAdSlotService _adSlotService)
        {
            adService = _adService;
            adSlotService = _adSlotService;
        }
        #region 广告
        [RoleAuthorize("AdList")]
        public IActionResult AdList(string search = "",string adSlotID = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<Ad, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(adSlotID))
                    {
                        predicate = predicate.And(k => k.AdSlotID == adSlotID);
                    }
                    return Json(adService.GetAdPagedList(pageindex, pagesize, predicate));
                }
                ViewBag.AdSlotTypeDLL = adSlotService.GetAdSlotListDLL(false,true);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("AdAdd")]
        public IActionResult AdAdd()
        {
            try
            {
                Ad model = new Ad();
                ViewBag.AdSlotTypeDLL = adSlotService.GetAdSlotListDLL();
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("AdAdd")]
        public IActionResult AdAdd(Ad model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(adService.AddAd(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("AdDel")]
        public IActionResult AdDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(adService.UpdateAdState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("AdEdit")]
        public IActionResult AdEdit(string id)
        {
            try
            {
                ResultResponse<Ad> result = adService.GetAd(id);
                ViewBag.AdSlotTypeDLL = adSlotService.GetAdSlotListDLL();
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("AdEdit")]
        public IActionResult AdEdit(Ad model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(adService.UpdateAd(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 广告位
        [RoleAuthorize("AdSlotList")]
        public IActionResult AdSlotList(string search = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<AdSlot, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    return Json(adSlotService.GetAdSlotPagedList(pageindex, pagesize, predicate));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("AdSlotAdd")]
        public IActionResult AdSlotAdd()
        {
            try
            {
                AdSlot model = new AdSlot();
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("AdSlotAdd")]
        public IActionResult AdSlotAdd(AdSlot model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(adSlotService.AddAdSlot(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("AdSlotDel")]
        public IActionResult AdSlotDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(adSlotService.UpdateAdSlotState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("AdSlotEdit")]
        public IActionResult AdSlotEdit(string id)
        {
            try
            {
                ResultResponse<AdSlot> result = adSlotService.GetAdSlot(id);
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("AdSlotEdit")]
        public IActionResult AdSlotEdit(AdSlot model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(adSlotService.UpdateAdSlot(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

    }
}